package top.lingchen.quickstart.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @Author 凌宸
 * @create 2021-12-13 下午 6:09
 * @Description
 * @Version 1.0
 */
@Slf4j
@Component // 别忘了将处理器加到 IOC 容器中
public class MyMetaObjectHandler implements MetaObjectHandler {
    // 插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert......");
        LocalDateTime currenTime = LocalDateTime.now();
        this.setFieldValByName( "createTime", new Date(), metaObject);
        this.setFieldValByName( "updateTime", new Date(), metaObject);
    }
    // 插更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update......");
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}
